package com.rupertjones.globalcron.domain;

import org.springframework.stereotype.Repository;

import java.util.Collection;

import static org.hibernate.criterion.Restrictions.eq;

/**
 * <p>&copy Rupert Jones 2011</p>
 *
 * @author rup
 */
@Repository
public class JobDescriptorDAO extends AbstractHibernateDAO<JobDescriptor> {
    public JobDescriptorDAO() {
        super(JobDescriptor.class);
    }

    @SuppressWarnings("unchecked")
    public Collection<JobDescriptor> getAllJobs() {
        return getCriteria()
                .add(eq("deleted", false))
                .list();
    }

    @SuppressWarnings("unchecked")
    public Collection<JobDescriptor> getAllJobsForHost(String hostname) {
        return getCriteria()
                .createAlias("host", "host")
                .add(eq("host.hostname", hostname))
                .add(eq("deleted", false))
                .list();
    }

    public Collection<JobDescriptor> getAllJobsForHost() {
        return getAllJobsForHost(Host.getCurrentHost());
    }

    @SuppressWarnings("unchecked")
    public Collection<JobDescriptor> getDisabledJobs() {
        return getCriteria()
                .add(eq("enabled", false))
                .add(eq("deleted", false))
                .list();
    }

    @SuppressWarnings("unchecked")
    public Collection<JobDescriptor> getDisabledJobsForHost(String hostname) {
        return getCriteria()
                .createAlias("host", "host")
                .add(eq("host.hostname", hostname))
                .add(eq("enabled", false))
                .add(eq("deleted", false))
                .list();
    }

    @SuppressWarnings("unchecked")
    public Collection<JobDescriptor> getDisabledJobsForHost(Host host) {
        return getDisabledJobsForHost(host.getHostname());
    }

    @SuppressWarnings("unchecked")
    public Collection<JobDescriptor> getDisabledJobsForHost() {
        return getDisabledJobsForHost(Host.getCurrentHost());
    }

    @SuppressWarnings("unchecked")
    public Collection<JobDescriptor> getEnabledJobs() {
        return getCriteria()
                .add(eq("enabled", true))
                .add(eq("deleted", false))
                .list();
    }

    @SuppressWarnings("unchecked")
    public Collection<JobDescriptor> getEnabledJobsForHost(String hostname) {
        return getCriteria()
                .createAlias("host", "host")
                .add(eq("host.hostname", hostname))
                .add(eq("enabled", true))
                .add(eq("deleted", false))
                .list();
    }

    @SuppressWarnings("unchecked")
    public Collection<JobDescriptor> getEnabledJobsForHost(Host host) {
        return getEnabledJobsForHost(host.getHostname());
    }

    public Collection<JobDescriptor> getEnabledJobsForHost() {
        return getEnabledJobsForHost(Host.getCurrentHost());
    }

    public void logicalDelete(JobDescriptor job) {
        job.setDeleted(true);
        upsert(job);
    }
}
